<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="./css/common.css">
    <!-- <link rel="stylesheet" href="./css/list.css"> -->
    <script src="./js/jquery-3.6.3.min.js"></script>
    <script src="./js/common.js"></script>
    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="./js/jquery-3.6.3.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="./editor.md/lib/marked.min.js"></script>
    <script src="./editor.md/lib/prettify.min.js"></script>
</head>

<body>
    <!--导航栏-->
    <div class="nav">
        <!--logo-->
        <img src="./images/book.png" alt="">
        <div class="content">
            <span>我的博客系统</span>
            <div class="opts">
                <a href="./blog_list.html">主页</a>
                <a href="./blog_eidt.html">写博客</a>
                <a href="logout">注销</a>
            </div>
        </div>
    </div>
    <!--正文部分-->
    <div class="container">
        <!--左边区域-->
        <div class="container-left">
            <div class="card">
                <img src="./images/003.jpg" alt="">
                <h3> </h3>
                <a href="#">GitHub</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <divc class="counter">
                    <span>2</span>
                    <span>1</span>
                </divc>
            </div>
        </div>
        <!--右边区域-->
        <div class="container-right">
            <!-- 博客正文 -->
            <div class="blog-content">
                <div class="blog-title">
                </div>
                <div class="blog-datetime">
                </div>
                <div class="content" id="content" style="background-color: transparent;">
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    // 发送请求获取博客详情
    $.ajax({
        // 方法
        type: 'get',
        // 拼装路径
        url: 'blog' + location.search,
        // 回调方法
        success: function (data) {
            // 设置页面上的值
            // title, 用 webapi 设置
            let titleEl = document.querySelector('.blog-content>.blog-title');
            console.log(titleEl);
            titleEl.innerHTML = data.title;
            // 时间，用jquery设置
            $('.blog-content>.blog-datetime').html(formatDate(data.createTime));
            // 正文
            // $('.blog-content>.content').html(data.content);
            //让内容以markdown的形式展示
            editormd.markdownToHTML('content', { markdown: data.content });
        }
    });
    $.ajax({
        type: 'get',
        url: 'user' + location.search,
        success: function (data) {
            console.log("检验通过...");
            //获取用户名标签 替换成具体用户名的值
            let usernameEl = document.querySelector('.container-left>.card>h3');
            usernameEl.innerHTML = data.username;
            if (data.isAuthor == 1) {
                //构件一个删除按钮
                let deleteEl = document.createElement('a');
                deleteEl.herf = '#';
                deleteEl.innerHTML = '删除';
                //把删除按钮加入到页面中
                let optsEl = document.querySelector('.nav>.content>.opts');
                optsEl.appendChild(deleteEl);
                //添加点击事件
                deleteEl.onclick = deleteBlog;
            }
        },
        statusCode: {
            403: function () {
                location.assign('blog_login.html');
            }
        }
    });

    //删除博客的请求
    function deleteBlog() {
        console.log('删除被调用');
        $.ajax({
            type: 'delete',
            url: 'blog' + location.search,
            success: function () {
                console.log("删除成功" + location.search);
                //跳转到博客列表页
                // alert('删除成功');
                location.assign('blog_list.html');
            },
            error: function (data) {
                console.log(data);
            }
        });
    }

</script>

</html>